<?
if(!$_SESSION['memberID']){exit("<script>window.location='dlogin.html'</script>");}
include("../../com.inc.php");
include '../class/SimpleExcel.class.php';
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$InSide = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true")); //票付通内部接口地址
$OutSide=new SoapClient("http://121.43.119.39/open/openService/MXSE.wsdl",array('encoding' =>'UTF-8','cache_wsdl' => 0));//票付通外部接口地址
$sel="select dname,account from pft_member where id={$_SESSION['memberID']} LIMIT 1";
$GLOBALS['le']->query($sel);
$GLOBALS['le']->fetch_assoc();
// $account=$GLOBALS['le']->f("account"); //账号
$account=$_SESSION['saccount']; //账号
$oname=$GLOBALS['le']->f("dname");
$jk_pw = $InSide->Create_Conn_REC($account,$oname); //接口密码
$lid_aid = $_REQUEST['Tlid']; //景区id_供应商id
$las = explode("_",$lid_aid);
$lid = $las[0];  //景区id
$aid = $_REQUEST['aid'] == $las[1]?$_REQUEST['aid']:$las[1];  //供应商id
$tid = $_REQUEST['tvalue']; //门票id
$playtime = $_REQUEST['order_play']; //游玩时间
$endtime = $_REQUEST['order_endplay'];//截止时间
$price = 100;
$Tnum = $_REQUEST['Tnum']; //单数
if($Tnum > 1000){echo "订单总数不能超过100";exit;}
$Tpnum = $_REQUEST['Tpnum']; //张数
$pay = $_REQUEST['pay']; //支付方式 
$echo_t = $_REQUEST['echo_t']; //输出方式
$group_name = $_REQUEST['tgname']; //姓名
$tel = "12301"; //手机号

$sel_title="select title from uu_land where id=".$lid." limit 1";
$GLOBALS['le']->query($sel_title);
$GLOBALS['le']->fetch_assoc();
$ltitle=$GLOBALS['le']->f("title");

//TODO::打开文件流
$file = $file ? $file: "/var/www/html/new/d/logs/exportCode/new_export_{$_SESSION['saccount']}.txt";
$fp = fopen($file,"a");


$UUcode = array();
$today = date('YmdHis');
write_logs($fp, '---------------日志记录开始:'.date('YmdHis').'-------------------');
$str=<<<h
"ac"=>$account,"pw"=>$jk_pw,"in0"=>$lid,"in1"=>$tid,"in2"=>$today,"in3"=>$price,"in4"=>$Tpnum,"in5"=>$playtime,"in6"=>$group_name,"in7"=>$tel,"in8"=>"","in9"=>1,"in10"=>$pay,"in11"=>0,"in12"=>"","in13"=>"","in14"=>0,"in15"=>0,"in16"=>$aid;$Tnum
h;
//write_logs($fp, $str);
$order_info = ''; 
/*
$submit_array = array(
    "ac"=>$account,
    "pw"=>$jk_pw,
    "in0"=>$lid,
    "in1"=>$tid,
    "in2"=>$today.$i,
    "in3"=>$price,
    "in4"=>$Tpnum,
    "in5"=>$playtime,
    "in6"=>$group_name,
    "in7"=>$tel,
    "in8"=>"",
    "in9"=>1,
    "in10"=>$pay,
    "in11"=>0,
    "in12"=>"",
    "in13"=>"",
    "in14"=>0,
    "in15"=>0,
    "in16"=>$aid,
);
var_export($submit_array);
exit;
*/
for($i=1;$i<=$Tnum;$i++){
    $submit_array = array(
        "ac"=>$account,
        "pw"=>$jk_pw,
        "in0"=>$lid,
        "in1"=>$tid,
        "in2"=>$today.$i,
        "in3"=>$price,
        "in4"=>$Tpnum,
        "in5"=>$playtime,
        "in6"=>$group_name,
        "in7"=>$tel,
        "in8"=>"",
        "in9"=>1,
        "in10"=>$pay,
        "in11"=>0,
        "in12"=>"",
        "in13"=>"",
        "in14"=>0,
        "in15"=>0,
        "in16"=>$aid,
    );
    write_logs($fp, json_encode($submit_array));
	// print_r($submit_array );exit;
    try {
        $submit_order = $OutSide->__soapCall("PFT_Order_Submit", $submit_array);
        $submit_order_xml = simplexml_load_string($submit_order);
        if ( $submit_order_xml->Rec->UUordernum) {
            $UUcode[$i]['b']=(string)$submit_order_xml->Rec->UUcode;
            // $UUcode = (string)$submit_order_xml->Rec->UUcode;
            // $UUcode[$i]['a']=(int)$submit_order_xml->Rec->UUordernum;
//            if(substr($submit_order_xml->Rec->UUcode, 0, 1) == 0 && $echo_t == 2)
//            {
//                $UUcode[$i]['b']="'".(string)$submit_order_xml->Rec->UUcode;
//            } else{
//                $UUcode[$i]['b']=(string)$submit_order_xml->Rec->UUcode;
//            }
        }
        else{
            // $UUcode = (string)$submit_order_xml->Rec->UUerrorcode;
            // $UUcode[$i]['a']="下单错误，请联系技术。";
            $UUcode[$i]['b']=(int)$submit_order_xml->Rec->UUerrorcode;
        }
        $order_info .= (int)$submit_order_xml->Rec->UUordernum.":".(string)$submit_order_xml->Rec->UUcode.'\n';
        // sleep(1);
        usleep(50);
    } catch(Exception $e) {
        //write_logs($fp, "soap error:" . $e->getMessage());
        continue;
    }

}
//TODO::关闭文件流
fclose($fp);
// exit;
// write_logs($order_info);
// write_logs('---------------日志记录结束:'.date('YmdHis').'-------------------');
if($echo_t == 1){		
	//txt导出
	Txt_Output($ltitle,$Tnum);
	foreach($UUcode as $k=>$v){
		print_r($v['b']);
		echo "\r\n";
	}
}elseif($echo_t == 2){
	//excel导出
	$filename= $ltitle.'('.$Tnum.')'.date('Ymd').'团购导码';
	$xls = new SimpleExcel('UTF-8', true,'lt');
	$xls->addArray($UUcode);
	$xls->generateXML($filename);
}
//导出模块txt
function Txt_Output($ltitle,$Tnum){
	$filename= $ltitle.'('.$Tnum.')'.date('Ymd').'团购导码.txt';
	header("Content-type: text");
	header("Content-Disposition: attachment; filename=$filename");
	header("Expires: 0");
	header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
	header("Pragma: public");		
}
// 日志记录
function write_logs($fp, $txt,$file=''){
//	$file = $file ? $file: "/var/www/html/new/d/logs/exportCode/export_{$_SESSION['saccount']}.txt";
//	$fp = fopen($file,"a");
	flock($fp, LOCK_EX);
	fwrite($fp,$txt."\n");
	flock($fp, LOCK_UN);
//	fclose($fp);
}
	
?>